package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.events.UserLoadandRegisterEvent.LoginEvent;
	import com.tongji.housediy.events.commonEvent.ShowComplementInforEvent;
	import com.tongji.housediy.events.houseEvent.OpenHouseEvent;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.utils.PopUpAdmin;
	
	import flash.events.MouseEvent;
	
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/6/27
	 */ 
	public class EntryTitleMediator extends Mediator
	{
		[Inject]
		public var entryWindow:EntryTitleWindow;		
		
		[Inject]
		public var roomModel:ExchangeDataModel;
		
		[Inject]
		public var userModel:UserInforModel;
		
		override public function onRegister():void
		{
			entryWindow.closeBtn.addEventListener(MouseEvent.CLICK, closeWindowHandler);
			entryWindow.newDesignBtn.addEventListener(MouseEvent.CLICK, newDesignHandler);
			entryWindow.openDesignBtn.addEventListener(MouseEvent.CLICK, openDesignHandler);
			entryWindow.selectHouseBtn.addEventListener(MouseEvent.CLICK, selectHouseHandler);
			entryWindow.sampleRoomBtn.addEventListener(MouseEvent.CLICK, sampleRoomHandler);
		}
		
		private function closeHandler():void
		{
			PopUpManager.removePopUp(entryWindow);
			mediatorMap.removeMediatorByView(entryWindow);
			//dispatch(new ShowComplementInforEvent(ShowComplementInforEvent.SHOW_COMPLEMENT_INFOR));
		}
		
		private function closeWindowHandler(event:MouseEvent):void
		{
		    closeHandler();
			if(entryWindow.name != "haveStarted")
				dispatch(new ShowComplementInforEvent(ShowComplementInforEvent.SHOW_COMPLEMENT_INFOR));
		}
		
		private function newDesignHandler(event:MouseEvent):void
		{
			var roomCount:int = roomModel.gridContainer.numChildren;
			for(var i:int; i < roomCount; i++)
			{
				roomModel.gridContainer.removeChildAt(roomModel.gridContainer.numChildren - 1);
			}
			roomModel.isOpened = false;
			closeHandler();	
			if(entryWindow.name != "haveStarted")
				dispatch(new ShowComplementInforEvent(ShowComplementInforEvent.SHOW_COMPLEMENT_INFOR));
		}
		
		private function openDesignHandler(event:MouseEvent):void
		{			

			/*var enterWindow:LoginTitleWindow = new LoginTitleWindow();
			enterWindow.name = "open";
			//enterWindow.name = "openFromEnter";
			PopUpManager.addPopUp(enterWindow, entryWindow.parent, true);
			PopUpManager.centerPopUp(enterWindow);
			mediatorMap.createMediator(enterWindow);*/
           if (!userModel.isLogined)
			{
				var enterWindow:LoginTitleWindow = new LoginTitleWindow();
				enterWindow.name = "openDesign";
				PopUpManager.addPopUp(enterWindow, entryWindow.parent, true);
				PopUpManager.centerPopUp(enterWindow);
				mediatorMap.createMediator(enterWindow);
			}
			else
			{
				PopUpAdmin.popupCenterWindow("openDesignTitle", entryWindow.parent, mediatorMap);
			}

			closeHandler();
		}
				
		private function selectHouseHandler(event:MouseEvent):void
		{
			dispatch(new OpenHouseEvent(OpenHouseEvent.GAT_ALL_HOUSES, 1, 6,0));           
			var cantavilWindow:CantavilTitleWindow = new CantavilTitleWindow();
			PopUpManager.addPopUp(cantavilWindow, entryWindow.parent, true);
			PopUpManager.centerPopUp(cantavilWindow);
			if(this.entryWindow.name == "haveStarted")
				cantavilWindow.name = "haveStarted";
			mediatorMap.createMediator(cantavilWindow);
			closeHandler();
		}
		
		private function sampleRoomHandler(event:MouseEvent):void
		{
			dispatch(new OpenHouseEvent(OpenHouseEvent.GET_SAMPLE_HOUSE, 1, 4, 0));
			PopUpAdmin.popupCenterWindow("sampleHouseTitle", entryWindow.parent, mediatorMap);
			closeHandler();
		}
	}
}